<?php

class Model_User {
	
	protected $user_id = 0;
	protected $registration_date;
	protected $modified_date;
	protected $password;
	protected $secret;
	protected $email;
	protected $enabled = 1;
	protected $active = 0;
	protected $pass_code;
	protected $pass_date;
	protected $pass_question;
	protected $pass_answer;
	protected $pass_ip;
	protected $permissions;
	protected $company = 0;
	protected $comments;
	protected $initial;
	protected $first_name;
	protected $middle_name;
	protected $last_name;
	protected $mobile;
	protected $phone;
	protected $info;
	protected $website;
	protected $address_line_one;
	protected $address_line_two;
	protected $postcode;
	protected $city;
	protected $county;
	
	public function __construct(array $options = null) {
		if (is_array ( $options )) {
			$this->setOptions ( $options );
		}
	}
	
	public function __set($name, $value) {
		$method = 'set' . ucfirst ( $name );
		if (('mapper' == $name) || ! method_exists ( $this, $method )) {
			throw new Exception ( 'Invalid content property' );
		}
		$this->$method ( $value );
	}
	
	public function __get($name) {
		$method = 'get' . ucfirst ( $name );
		if (('mapper' == $name) || ! method_exists ( $this, $method )) {
			throw new Exception ( 'Invalid content property' );
		}
		return $this->$method ();
	}
	
	public function setOptions(array $options) {
		$methods = get_class_methods ( $this );
		foreach ( $options as $key => $value ) {
			$method = 'set' . ucfirst ( $key );
			if (in_array ( $method, $methods )) {
				$this->$method ( $value );
			}
		}
	}
	
	public function getURI() {
	
	}
	
	public function getActivationURI() {
		if ($this->getSecret () == '') {
			$this->setSecret ( sha1 ( $this->getPassword () ) );
			$this->save ();
		}
		return site_url () . 'user/activate/' . $this->getSecret ();
	}
	
	public function getByEmail() {
		return Model_Mapper_User::newInstance ()->getByEmail ( $this->getEmail () );
	}
	public function setUser_id($value) {
		$this->user_id = $value;
	}
	
	public function getUser_id() {
		return $this->user_id;
	}
	
	public function setRegistration_date($value) {
		$this->registration_date = $value;
	}
	
	public function getRegistration_date() {
		return $this->registration_date;
	}
	
	public function setModified_date($value) {
		$this->modified_date = $value;
	}
	
	public function getModified_date() {
		return $this->modified_date;
	}
	
	public function setPassword($value) {
		$this->password = $value;
	}
	
	public function getPassword() {
		return $this->password;
	}
	
	public function setSecret($value) {
		$this->secret = $value;
	}
	
	public function getSecret() {
		return $this->secret;
	}
	
	public function setEmail($value) {
		$this->email = $value;
	}
	
	public function getEmail() {
		return $this->email;
	}
	
	public function setEnabled($value) {
		$this->enabled = $value;
	}
	
	public function getEnabled() {
		return $this->enabled;
	}
	
	public function setActive($value) {
		$this->active = $value;
	}
	
	public function getActive() {
		return $this->active;
	}
	
	public function setPass_code($value) {
		$this->pass_code = $value;
	}
	
	public function getPass_code() {
		return $this->pass_code;
	}
	
	public function setPass_date($value) {
		$this->pass_date = $value;
	}
	
	public function getPass_date() {
		return $this->pass_date;
	}
	
	public function setPass_question($value) {
		$this->pass_question = $value;
	}
	
	public function getPass_question() {
		return $this->pass_question;
	}
	
	public function setPass_answer($value) {
		$this->pass_answer = $value;
	}
	
	public function getPass_answer() {
		return $this->pass_answer;
	}
	
	public function setPass_ip($value) {
		$this->pass_ip = $value;
	}
	
	public function getPass_ip() {
		return $this->pass_ip;
	}
	
	public function setPermissions($value) {
		$this->permissions = $value;
	}
	
	public function getPermissions() {
		return $this->permissions;
	}
	
	public function setCompany($value) {
		$this->company = $value;
	}
	
	public function getCompany() {
		return $this->company;
	}
	
	public function setComments($value) {
		$this->comments = $value;
	}
	
	public function getComments() {
		return $this->comments;
	}
	
	public function setInitial($value) {
		$this->initial = $value;
	}
	
	public function getInitial() {
		return $this->initial;
	}
	
	public function setFirst_name($value) {
		$this->first_name = $value;
	}
	
	public function getFirst_name() {
		return $this->first_name;
	}
	
	public function setMiddle_name($value) {
		$this->middle_name = $value;
	}
	
	public function getMiddle_name() {
		return $this->middle_name;
	}
	
	public function setLast_name($value) {
		$this->last_name = $value;
	}
	
	public function getLast_name() {
		return $this->last_name;
	}
	
	public function setMobile($value) {
		$this->mobile = $value;
	}
	
	public function getMobile() {
		return $this->mobile;
	}
	
	public function setPhone($value) {
		$this->phone = $value;
	}
	
	public function getPhone() {
		return $this->phone;
	}
	
	public function setInfo($value) {
		$this->info = $value;
	}
	
	public function getInfo() {
		return $this->info;
	}
	
	public function setWebsite($value) {
		$this->website = $value;
	}
	
	public function getWebsite() {
		return $this->website;
	}
	
	public function setAddress_line_one($value) {
		$this->address_line_one = $value;
	}
	
	public function getAddress_line_one() {
		return $this->address_line_one;
	}
	
	public function setAddress_line_two($value) {
		$this->address_line_two = $value;
	}
	
	public function getAddress_line_two() {
		return $this->address_line_two;
	}
	
	public function setPostcode($value) {
		$this->postcode = $value;
	}
	
	public function getPostcode() {
		return $this->postcode;
	}
	
	public function setCity($value) {
		$this->city = $value;
	}
	
	public function getCity() {
		return $this->city;
	}
	
	public function setCounty($value) {
		$this->county = $value;
	}
	
	public function getCounty() {
		return $this->county;
	}
	
	public function save() {
		$date = date ( 'Y-m-d H:i:s' );
		$this->registration_date = $date;
		$this->modified_date = $date;
		if ($this->email != '' || $this->password != '') {
			Model_Mapper_User::newInstance ()->save ( $this );
		} else {
			throw new Exception ( 'Please ensure first name, email and password is not blank' );
		}
	
	}
	
	public function activate() {
		$this->setActive ( 1 );
		$this->save ();
	}
	
	public function sendActivatedEmail() {
	
	}
	public function sendActivationEmail() {
		$mailer = new Email_Manager ();
		$mailer->IsHTML ();
		$mailer->Subject = 'Activate Your Account-Myhighst.com';
		$mailer->AddReplyTo ( 'info@myhighst.com', 'Myhighst.com' );
		$mailer->SetFrom ( 'info@myhighst.com', 'Myhighst.com' );
		$mailer->AddAddress ( $this->getEmail (), $this->getFirst_name () );
		$msg = file_get_contents ( template_path () . 'email' . DS . 'activate.phtml' );
		$msg = str_replace ( '{first_name}', $this->first_name, $msg );
		$msg = str_replace ( '{link}', $this->getActivationURI (), $msg );
		$msg = str_replace ( '{link_text}', 'Click here to activate', $msg );
		$mailer->MsgHTML ( $msg );
		$mailer->Send ();
	}
	
	public function toArray() {
		return get_object_vars ( $this );
	}

}

